Skip to content

Conversation

@kotaitos
Copy link
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
The adk deploy commands (cloud_run, agent_engine, gke) were not properly respecting .gitignore, .gcloudignore, or .ae_ignore files. This caused all files in the source directory, including large or sensitive ones like venv/, .git/, and .env, to be copied to the temporary staging directory and subsequently uploaded to hosted environments.

Solution:

  • Implemented a unified _get_ignore_patterns_func helper in src/google/adk/cli/cli_deploy.py that reads and combines patterns from .gitignore, .gcloudignore, and .ae_ignore.
  • Updated to_cloud_run, to_agent_engine, and to_gke to use this helper as an ignore filter in shutil.copytree.
  • This ensures that only the files intended by the user are staged and deployed.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Summary of pytest results:

tests/unittests/cli/utils/test_cli_deploy_ignore.py ... [100%]
3 passed in 2.20s

Manual End-to-End (E2E) Tests:

Verified the fix by following these steps:

  1. Setup: Created a dummy agent directory with a .gitignore file.
    mkdir -p verify_agent
    touch verify_agent/agent.py verify_agent/__init__.py
    touch verify_agent/ignored_file.txt
    echo "ignored_file.txt" > verify_agent/.gitignore
  2. Execution: Ran the deploy command pointing to a local temp folder.
    adk deploy cloud_run --temp_folder ./debug_staged_out ./verify_agent
  3. Verification: Temporarily disabled the shutil.rmtree cleanup in code to inspect ./debug_staged_out.
  4. Result: Confirmed that agent.py and .gitignore were present, but ignored_file.txt was correctly excluded from the staging area.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

The adk deploy commands (cloud_run, agent_engine, gke) were not properly
respecting .gitignore, .gcloudignore, or .ae_ignore files, causing
unwanted files (like venv, .git, etc.) to be uploaded.

This change:
- Adds a unified _get_ignore_patterns_func helper that reads all three ignore files.
- Updates to_cloud_run, to_agent_engine, and to_gke to use this helper.
- Removes hardcoded ignore patterns to strictly follow user configuration.
- Adds comprehensive unit tests to verify the fix.

Fixes google#4183
@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Jan 17, 2026
@kotaitos
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a great improvement by making the adk deploy commands respect various ignore files (.gitignore, .gcloudignore, .ae_ignore). The implementation is clean, centralizing the logic in a new _get_ignore_patterns_func helper. This function is then correctly applied to to_cloud_run, to_agent_engine, and to_gke deployment functions. The refactoring in to_agent_engine is particularly nice as it replaces specific logic with the new generic helper. The addition of comprehensive unit tests in test_cli_deploy_ignore.py ensures the new functionality is well-tested and robust.

I have one suggestion to make the error handling in the new helper function more specific and robust. Overall, this is a solid contribution.

Comment on lines +514 to +524
try:
with open(filepath, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
# If it ends with /, remove it for fnmatch compatibility
if line.endswith('/'):
line = line[:-1]
patterns.add(line)
except Exception as e:
click.secho(f'Warning: Failed to read {filename}: {e}', fg='yellow')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This try...except block can be made more robust.

  1. Specify file encoding: It's good practice to explicitly specify the file encoding when opening text files to ensure consistent behavior across different systems. utf-8 is a safe default for ignore files.
  2. Use specific exceptions: Catching the generic Exception is too broad and can hide unexpected bugs. It's better to catch more specific exceptions, such as OSError for file I/O problems and UnicodeDecodeError when an encoding is specified.
Suggested change
try:
with open(filepath, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
# If it ends with /, remove it for fnmatch compatibility
if line.endswith('/'):
line = line[:-1]
patterns.add(line)
except Exception as e:
click.secho(f'Warning: Failed to read {filename}: {e}', fg='yellow')
try:
with open(filepath, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'):
# If it ends with /, remove it for fnmatch compatibility
if line.endswith('/'):
line = line[:-1]
patterns.add(line)
except (OSError, UnicodeDecodeError) as e:
click.secho(f'Warning: Failed to read {filename}: {e}', fg='yellow')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adk deploy cloud_run does not respect .gitignore or .gcloudignore

3 participants